package com.wjs.controller;

import com.wjs.entity.Brand;
import com.wjs.entity.Phone;
import com.wjs.service.BrandService;
import com.wjs.service.PhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/phone")
public class PhoneController {
    @Autowired
    private PhoneService phoneService;
    @Autowired
    private BrandService brandService;

    @RequestMapping("/selectAll")
    public String selectAll(Model model){
        List<Phone> phones = phoneService.selectAll();
        model.addAttribute("phones",phones);
        return "/phone-list.jsp";
    }

    @RequestMapping("/addPhone")
    public String insertPhone(Phone phone){
        phoneService.insertPhone(phone);
        return "redirect:/phone/selectAll.do";
    }

    @RequestMapping("deletePhone")
    public String deletePhoneById(Integer id){
        phoneService.deletePhoneById(id);
        return "redirect:/phone/selectAll.do";
    }

    @RequestMapping("selectById")
    public String selectById(Integer id,Model model){
        Phone phone = phoneService.selectPhoneById(id);
        List<Brand> brands = brandService.selectAll();
        model.addAttribute("phone",phone);
        model.addAttribute("brands",brands);
        return "/updatephone.jsp";
    }

    @RequestMapping("deleteMany")
    public String deleteMany(int []ids){
        phoneService.deleteMany(ids);
        return "redirect:/phone/selectAll.do";
    }

    @RequestMapping("update")
    public String update(Phone phone){
        phoneService.updatePhone(phone);
        return "redirect:/phone/selectAll.do";
    }
}
